#include "PreGraphic.h"
#include "Scene.h"
#include "RenderSystem.h"
#include "Actor.h"
#include "Camera.h"
#include "Sun.h"

Scene::Scene(void)
{
}

Scene::~Scene(void)
{
}

void Scene::draw(RenderSystem* rs,Camera* camera)
{
	rs->getD3DDevice()->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00aaaaaa,1.0f, 0);
	rs->setProjectionMatrix(camera->getProjectionMatrix());
	rs->setViewMatrix(camera->getViewMatrix());

	initLight(rs);

	for(ActorList::iterator ite = actors.begin();ite!=actors.end();++ite){
		Actor* actor = *ite;
		if(actor->isVisible()){
			actor->draw(rs);
		}
	}
	rs->getD3DDevice()->Present(0, 0, 0, 0);
}

void Scene::frameMove()
{
	
}

void Scene::insertActor(Actor* actor)
{
	actors.push_back(actor);
}

void Scene::setSun(Sun* s)
{
	sun = s;
}

void Scene::initLight(RenderSystem* rs)
{
	if(sun){
		D3DLIGHT9 light;
		::ZeroMemory(&light,sizeof(light));
		light.Type = D3DLIGHT_DIRECTIONAL;
		memcpy(&light.Ambient ,&sun->getAmbient(),sizeof(D3DCOLORVALUE));
		memcpy(&light.Diffuse ,&sun->getDiffuse(),sizeof(D3DCOLORVALUE));
		memcpy(&light.Specular ,&sun->getSpecular(),sizeof(D3DCOLORVALUE));
		memcpy(&light.Direction ,&sun->getDirection(),sizeof(D3DVECTOR));
		rs->getD3DDevice()->SetLight(0,&light);
		if(sun->isVisible())
			rs->getD3DDevice()->LightEnable(0,true);
		else
			rs->getD3DDevice()->LightEnable(0,false);
	}
}